import csv
import time
import random
import re

import requests


def PageGenerator(n):
    return "https://beijing.anjuke.com/sale/p{}-rd1/#filtersort".format(n)


if __name__ == "__main__":

    with open('results.csv', 'w', newline='') as csvfile:
        spamwriter = csv.writer(csvfile,
                                delimiter='|',
                                quotechar='^',
                                quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow(("标题", "房间", "面积", "楼层", "年龄", "小区", "地址", "价格"))

        headers = {
            'user-agent':
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0'
        }

        for i in range(10):
            page = PageGenerator(i + 1)
            response = requests.get(page, headers=headers)
            html = response.content.decode("UTF-8")

            def extract(info: str):
                return re.findall(r'<span>(.*?)</span>', info)

            title = re.findall(r'class="houseListTitle ">[\n.]\s+(.*?)</a>',
                               html)

            details = re.findall(
                r'class="details-item">[\n.]\s+(.*?)\s+[\n.]\s+</div>', html)
            details = map(extract, details)
            details = list(details)

            addr = re.findall(r'class="comm-address"[\n.]\s+title="(.*?)">',
                              html)
            addr = map(lambda x: x.split("&nbsp;&nbsp;"), addr)
            addr = list(addr)
            price = re.findall(r'price-det"><strong>(.*?)</strong>', html)

            for j in range(len(title)):
                result = (title[j], details[j][0], details[j][1],
                          details[j][2], details[j][3], addr[j][0], addr[j][1],
                          price[j])
                spamwriter.writerow(result)
                print("{} has been written to csv".format(result))

            time.sleep(random.randint(20, 30))
